package com.telit.firecommand.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.telit.common.entity.DataReturn;
import com.telit.common.entity.PageData;
import com.telit.firecommand.entity.CommEmergencyPlan;
import com.telit.firecommand.entity.PlanType;
import com.telit.firecommand.service.EmergencyPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2021/8/3 9:38
 */
@RestController
@RequestMapping("/emergency-plan")
@Api(tags = "应急预案表", description = "/emergency-plan")
public class EmergencyPlanController {

    @Autowired
    private EmergencyPlanService emergencyPlanService;

    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @ApiOperation("新增应急预案信息")
    public DataReturn add(@Validated CommEmergencyPlan emergencyPlan ) {
        emergencyPlanService.add(emergencyPlan);
        return DataReturn.ok();
    }

    @GetMapping(value = "/listByPage", produces = "application/json;charset=UTF-8")
    @ApiOperation("分页条件查询应急预案信息")
    public DataReturn listByPage(@RequestParam Integer current,
                                 @RequestParam Integer size,
                                 @RequestParam(required = false) String startTime,
                                 @RequestParam(required = false) String endTime,
                                 @RequestParam(required = false) String planName,
                                 @RequestParam(required = false) String planType,
                                 @RequestParam(required = false) String fileCode) {
        IPage<CommEmergencyPlan> emergencyPlanIPage = emergencyPlanService.selectByPage(current, size, startTime,
                endTime, planName, planType, fileCode);
        //转换文件预览地址
        List<CommEmergencyPlan> emergencyPlanList = emergencyPlanIPage.getRecords();
        return DataReturn.ok(new PageData<>((int) emergencyPlanIPage.getTotal(), current, size, emergencyPlanList));
    }

    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
    @ApiOperation("无分页条件查询应急预案信息")
    public DataReturn list(@RequestParam(required = false) String startTime,
                           @RequestParam(required = false) String endTime,
                           @RequestParam(required = false) String planName,
                           @RequestParam(required = false) String planType,
                           @RequestParam(required = false) String fileCode) {
        List<CommEmergencyPlan> commEmergencyPlanList = emergencyPlanService.list(startTime, endTime, planName,
                planType, fileCode);
        return DataReturn.ok(commEmergencyPlanList);
    }

    @GetMapping(value = "/detail/{id}", produces = "application/json;charset=UTF-8")
    @ApiOperation("查询详细信息")
    public DataReturn<CommEmergencyPlan> detail(@PathVariable("id") Long id) {
        return DataReturn.ok(emergencyPlanService.info(id));
    }

    @PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
    @ApiOperation("修改应急预案信息")
    public DataReturn update(@Validated CommEmergencyPlan emergencyPlan) {
        emergencyPlanService.updateData(emergencyPlan);
        return DataReturn.ok();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "根据ID删除信息发布表")
    public DataReturn delete(@PathVariable("id") Long id) {
        emergencyPlanService.deleteById(id);
        return DataReturn.ok();
    }

    @GetMapping("/deleteBatch")
    @ApiOperation(value = "根据ID数组删除应急预案信息表")
    @ApiImplicitParam(name = "ids", value = "配置ID数组", paramType = "query", dataType = "Long[]")
    public DataReturn deleteBatch(Long[] ids) {
        List<Integer> values = emergencyPlanService.deleteBatch(ids);
        return DataReturn.ok(values);
    }



    @PostMapping(value = "/addPlanType", produces = "application/json;charset=UTF-8")
    @ApiOperation("新增应急预案类型")
    public DataReturn addPlanType(@RequestBody PlanType planType) {
        emergencyPlanService.addPlanType(planType);
        return DataReturn.ok();
    }


    @GetMapping(value = "/deletePlanType")
    @ApiOperation("删除应急预案类型")
    public DataReturn deletePlanType(@RequestParam("id") String id) {
        emergencyPlanService.deletePlanType(id);
        return DataReturn.ok();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "planLevel", value = "火灾等级", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "planPeople", value = "伤亡人数", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "planFireArea", value = "灾害面积", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/match", produces = "application/json;charset=UTF-8")
    @ApiOperation("查询匹配列表")
    public DataReturn<PageData<List<CommEmergencyPlan>>> match(
            @RequestParam(required = false, defaultValue = "1") Integer current,
            @RequestParam(required = false, defaultValue = "20") Integer size,
            @RequestParam(required = false) String planType,
            @RequestParam(required = false) String planLevel,
            @RequestParam(required = false) Integer planPeople,
            @RequestParam(required = false) BigDecimal planFireArea) {
        return DataReturn.ok(emergencyPlanService.match(current, size, planType, planLevel, planPeople, planFireArea));
    }

}
